package com.dragon.client.controller.avttendance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.avttendance.IScheduleService;
import com.dragon.repository.domain.dto.ScheduleDTO;
import com.dragon.repository.domain.vo.ScheduleVO;
import com.dragon.repository.entity.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 日程表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-10
 */
@RestController
@RequestMapping("schedule")
@Api(tags = "日程管理")
public class ScheduleController {

    @Autowired
    IScheduleService service;


    @GetMapping("list")
    @ApiOperation("获取日程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, type = "int"),
            @ApiImplicitParam(name = "time", value = "年月", required = false, type = "string")
    })
    public ResponseData<Map<String, List<Schedule>>> ScheduleList(
            @RequestParam Integer adminId,
            @RequestParam(required = false) String time){
        Map<String, List<Schedule>> list = service.getList(adminId,time);
        return ResponseData.success(list);
    }

    @GetMapping("listBySearch")
    @ApiOperation("根据条件获取日程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码",required = true,type = "int"),
            @ApiImplicitParam(name = "limit", value = "页数", required = true, type = "int"),
            @ApiImplicitParam(name = "time", value = "时间", required = false, type = "string"),
            @ApiImplicitParam(name = "search", value = "搜索参数(title)", required = false,type = "string")
    })
    public ResponseData<List<ScheduleDTO>> listBySearch(@RequestParam Integer page, @RequestParam Integer limit,
                                                        @RequestParam(required = false) String time ,
                                                        @RequestParam(required = false) String search, HttpServletRequest request){
        Page<Schedule> list = service.listBySearch(page, limit, time, search,request);
        return ResponseData.success(ScheduleDTO.convertList(list.getRecords()));
    }

    @PostMapping("add")
    @ApiOperation("添加")
    public ResponseData addSchedule(@RequestBody @Validated(Insert.class)ScheduleVO vo){
        service.addSchedule(ScheduleVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation("更新")
    public ResponseData updateSchedule(@RequestBody @Validated(Update.class)ScheduleVO vo){
        service.updateSchedule(ScheduleVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public ResponseData deleteSchedule(@RequestParam Integer id){
        service.deleteSchedule(id);
        return ResponseData.success();
    }


    @GetMapping("listForDay")
    @ApiOperation(value = "用户某天的全部日程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, type = "int"),
            @ApiImplicitParam(name = "time", value = "年月日", required = false, type = "string")
    })
    public ResponseData<List<ScheduleDTO>> listForDay(
            @RequestParam Integer adminId,
            @RequestParam(required = false) String time){
        List<Schedule> serviceListForDay = service.getListForDay(adminId, time);
        return ResponseData.success(ScheduleDTO.convertList(serviceListForDay), (long) serviceListForDay.size());
    }

}
